var modelSlideshow = {
   id : '#jeslee-model-gallery',
   navId : '#jeslee-model-gallery-nav',
   params: {
      modelCount: 0,
      modelIndex: 1,
      thumbListWidth: 0
   },

   init : function () {
      $($ms.id + ' .meta-info').hide();
      $ms.params.modelCount = $($ms.id + ' .model-thumb-list li').size();
      $ms.params.modelIndex = 1;
      $ms.setSlideDimensions();
      $($ms.navId + ' .maxModels').html($ms.params.modelCount);
      $ms.showModel($($ms.id + ' .model-thumb-list li').first());
      $($ms.id + ' .model-thumb-list li a').click(
            function(event) {
               event.preventDefault();
               $ms.showModel($(this).parent());
            });
      $($ms.navId + ' .left').click($ms.slideLeft);
      $($ms.navId + ' .right').click($ms.slideRight);
   },

   showModel: function(modelImg) {
      if (modelImg && modelImg.length > 0) {
         if (modelImg.hasClass('selected'))
         {
            var imageUrl = $('a.large-model-impression', modelImg).attr('href');
            jeslee.log(imageUrl);
            $.slimbox(imageUrl);
         }
         else
         {
            // remove selected class from other models
            $($ms.id + " .model-thumb-list li").removeClass('selected');
            // set current model to selected
            modelImg.addClass('selected');
            var positionLeft = $(modelImg).position().left;
            $ms.slideModels(($($ms.id + ' .model-thumbs').outerWidth() / 2) - positionLeft - ($(modelImg).outerWidth(true) / 2));
            $('#jeslee-model-specs').html($('.meta-info', modelImg).html());
            var modelClass = $("a", modelImg).attr('id');
            var selectedIndex = modelClass.substring("model-impression-".length);
            $ms.setModelIndex(selectedIndex);
         }
      }
   },

   slideLeft: function() {
      $ms.showModel($($ms.id + " .model-thumb-list li.selected").prev());
   },
   slideRight: function() {
      $ms.showModel($($ms.id + " .model-thumb-list li.selected").next());
   },
   slideModels:function (posLeft) {
      if (isNaN(posLeft)) {
         posLeft = 0;
      }/* else {
         if (posLeft > 0) {
            posLeft = 0;
         }
         var minLeft = $($ms.id + ' .model-thumbs').outerWidth() - $ms.params.thumbListWidth;
         if (posLeft < minLeft) {
            posLeft = minLeft;
         }
      }*/
//      $($ms.id + ' .model-thumb-list').animate({left: posLeft}, 1200);
      $($ms.id + ' .model-thumb-list').css({left: posLeft});
   },
   setSlideDimensions: function() {
      var totalWidth = 0;
      $($ms.id + ' .collection-description').each(function() {
         totalWidth += $(this).outerWidth(true);
      });
      jeslee.log("totalWidth="+totalWidth);
      var maxHeight = 0;

      $($ms.id + ' .model-thumb-list li').each(function() {
         var imgLi = $(this);
         totalWidth += imgLi.outerWidth(true); // outerWidth inlcudes padding and border, the true param means include the margin also
         elementHeight = imgLi.outerHeight(true);
         if (elementHeight > maxHeight) {
            maxHeight = elementHeight;
         }
         //center image vertical
         // set wrapper element to position relative
         imgLi.css({position:'relative'});
         // position img absolute and calculate the top offset to it's parent.
         $("a img", imgLi).load(function() {
            $(this).css({position:'absolute'});
            $(this).css({top:(imgLi.height() - $(this).height())/2});
         });
      });
      $ms.params.thumbListWidth = totalWidth + 1; // +1 just to be on the save side :-)
      $($ms.id + ' .model-thumb-list').css({
         width: ($ms.params.thumbListWidth)
      });
      $($ms.id + ' .model-thumbs').css({
         height: maxHeight
      });
   },
   setModelIndex: function(index) {
      if (index < 1) {
         index = 1;
      }
      if (index > $ms.params.modelCount) {
         index = $ms.params.modelCount;
      }
      $ms.params.modelIndex = index;
      $($ms.navId + ' .modelIndex').html(index);
   }
};


$ms = modelSlideshow;